﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Data.Objects.DataClasses;

namespace Server
{
    public class RssFeedSync
    {
        private String _uri;
        private SyndicationFeed _feed;
        private DB _db;

        public RssFeedSync(String uri)
        {
            _db = new DB();
            _uri = uri;
            try
            {
                XmlReader reader = XmlReader.Create(_uri);
                _feed = SyndicationFeed.Load(reader);
            }
            catch (Exception e)
            {
                _feed = null;
            }
        }

        public RssFeed Save()
        {
            if (_feed != null)
            {
                ChannelEntity chan = new ChannelEntity();
                chan.date = DateTime.Now;
                chan.description = _feed.Description.Text;
                chan.title = _feed.Title.Text;
                chan.@public = false;
                chan.url = _uri;
                chan.link = _feed.Links.First().Uri.AbsoluteUri;
                var items = from i in _feed.Items.Take(10)
                            where i.PublishDate.Date >= DateTime.Today.AddDays(-5)
                            select i;
                chan.items = ItemToEntity(items);
                chan = _db.createChannel(chan);
                return ConvertEntityModel.ToRssFeed(chan);
            }
            return null;
        }

        private EntityCollection<ItemEntity> ItemToEntity(IEnumerable<SyndicationItem> items)
        {
            EntityCollection<ItemEntity> list = new EntityCollection<ItemEntity>();
            foreach (SyndicationItem item in items)
            {
                ItemEntity tmp = new ItemEntity();
                tmp.description = item.Summary.Text;
                tmp.date = item.PublishDate.DateTime;
                tmp.title = item.Title.Text;
                tmp.link = item.Links.First().Uri.AbsoluteUri;
                list.Add(tmp);
            }
            return list; ;
        }

        public RssFeed Refresh()
        {
            if (_feed != null)
            {
                ChannelEntity chan = _db.getChannel(_uri);
                if (chan != null)
                {
                   
                   var last_item = (from i in chan.items
                                    orderby i.date descending
                                    select i).FirstOrDefault();
                   if (last_item != null)
                   {
                       DateTime date = last_item.date;
                       var items = from i in _feed.Items
                                   where i.PublishDate.DateTime > date
                                   select i;
                       if (items.Count() > 0)
                           _db.updateChannel(chan, ItemToEntity(items));
                   }
                   else
                   {
                       var items = from i in _feed.Items.Take(10)
                                   where i.PublishDate.Date >= DateTime.Today.AddDays(-5)
                                   select i;
                       if (items.Count() > 0)
                           _db.updateChannel(chan, ItemToEntity(items));
                   }
                    return ConvertEntityModel.ToRssFeed(chan);
                }
            }
            return null;
        }
    }
}